<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\PersonalProfileVerify;
use App\Http\Controllers\Controller;
use App\Model\GuildManage;
use App\Model\Message;
use App\Model\User;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Util\ComUtil;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use App\Model\Guild\GuildHost;

class PersonalProfileController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('实名认证资料审核');
            $content->description('');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return Admin::grid(UserProfilePersonal::class, function (Grid $grid) {

            $adminId = Admin::user()->id;

            $where = [
                ['flag', '<>', UserProfilePersonal::STATUS_DEL],
                ['real_name', '<>', ''],
            ];
            if(in_array($adminId,[50,51])){
                $guidIds = GuildManage::getGuidIdByAdminId($adminId);
                $hostIds = GuildHost::getHostUidByAdminId($guidIds);
                $grid->model()->where($where)->whereIn('user_id',$hostIds)->orderBy('updated_at', 'desc');
            }else{
                $grid->model()->where($where)->orderBy('updated_at', 'desc');
            }

            $grid->userProfile()->user_profile_id('用户ID');
            $grid->userProfile()->nick_name('昵称');
            $grid->userProfile()->gender('性别')->display(function ($gender) {
                return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
            });
            $grid->real_name('姓名');
            $grid->id_card('身份证号');
            // $grid->alipay_account('支付宝账号');
            $grid->phone('手机号');
            $grid->id_card_pics('身份证资料')->display(function ($data) {
                if (!$data) return '';
                $pics = json_decode($data, true);
                return ComUtil::buidlImgaeHtml($pics);
            });
            $grid->face_vry_pic('认证实人照片')->display(function ($data) {
                if (!$data) return '-';
                return ComUtil::buidlImgaeHtml($data);
            });
            $grid->created_at('提交时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->updated_at('审核时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->vry_type('认证方式')->display(function ($data) {
                return $this->vry_type == 0 ? "阿里云认证" : "人工认证";
            });
            $grid->flag('审核结果')->display(function ($data) {
                return UserProfilePersonal::STATUS[$data];
            });

            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            // $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableBatchActions();

            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('userprofile.user_profile_id', '用户ID');
                    $filter->equal('id_card', '身份证');
                    $filter->equal('flag', '审核状态')->select(UserProfilePersonal::STATUS);
                    $filter->equal('userItem.status', '用户状态')->select(User::STATUS);
                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('vry_type', '认证方式')->select([0 => '阿里云认证', 1 => '人工认证']);
                    $filter->between('created_at', '提交时间')->datetime();
                    $filter->between('updated_at', '审核时间')->datetime();
                    $filter->equal('userprofile.real_name', '姓名');
                });
            });
            // $grid->setActionClass(Actions::class);
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();
                $actions->append(new PersonalProfileVerify(
                    $actions->getKey(),
                    $actions->row,
                    'personal_profile/verify'
                ));
            });
        });
    }

    function verify()
    {
        $id = request('id');
        $verify = request('status');
        $record = UserProfilePersonal::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];

        //更新记录
        $record->update([
            'flag' => $verify
        ]);

        if ($verify == UserProfilePersonal::STATUS_DEL) {
            return ['ret' => 0, 'message' => '操作成功！'];
        }

        //变成女用户身份
        $profile = UserProfile::getProfile($record->user_id);

        // $systemMsg = '恭喜你！你已成功开通个人技能，保持在线可获得更多金币！';

        if ($verify == UserProfilePersonal::STATUS_FAIL) {
//            $upInfo = [
//                'role' => UserProfile::ROLE_USER,
//            ];
//            UserProfile::updateInfo($record->user_id, $upInfo);
            $systemMsg = '抱歉！你提交的实名认证资料有误，请重新填写！';
        } elseif ($verify == UserProfilePersonal::STATUS_OK) {
//            $upInfo = [
//                'role' => UserProfile::ROLE_HOST,
//                'host_time' => time()
//            ];
            //初始化的收费设置
//            if ($profile->coin_setting < 1) $upInfo['coin_setting'] = 20;
//            if ($profile->video_coin_setting < 1) $upInfo['video_coin_setting'] = 40;
//            UserProfile::updateInfo($record->user_id, $upInfo);
            $systemMsg = '恭喜，你的实名认证信息已通过，你可以接听收费语音与收费视频通话，获得的收益将以金币形式发放到个人账户。我们倡导绿色健康的交友环境，严禁色情低俗，线下交易等行为，请遵守平台相关规定，营造良好的交友氛围。';
        }
        //发送系统提示
        Message::pushSystem($record->user_id, $systemMsg, '实名认证通知');
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
